package com.njkwt.business.system.controller;

import com.njkwt.business.system.dto.RelevanceDto;
import com.njkwt.business.system.dto.PageDto;
import com.njkwt.business.system.dto.ResponseDto;
import com.njkwt.business.system.service.RelevanceService;
import com.njkwt.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* @ClassName RelevanceController
* @Description
* @Author ydj (171799383@qq.com)
* @Date  2020-8-18 14:12:32
* @Version V1.0
**/

@RestController
@RequestMapping("/api/business/system/relevance")
public class RelevanceController {

    private static final Logger LOG = LoggerFactory.getLogger(RelevanceController.class);
    public static final String BUSINESS_NAME = "多对多关系集中映射";

    @Resource
    private RelevanceService relevanceService;

    /**
     * 列表查询
     */
    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();
        relevanceService.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody RelevanceDto relevanceDto) {
        // 保存校验
        ValidatorUtil.require(relevanceDto.getId(), "流水号");
        ValidatorUtil.length(relevanceDto.getId(), "流水号", 1, 50);
        ValidatorUtil.require(relevanceDto.getDescription(), "描述");
        ValidatorUtil.length(relevanceDto.getDescription(), "描述", 1, 100);
        ValidatorUtil.require(relevanceDto.getKey(), "映射标识");
        ValidatorUtil.length(relevanceDto.getKey(), "映射标识", 1, 100);
        ValidatorUtil.require(relevanceDto.getStatus(), "状态");
        ValidatorUtil.require(relevanceDto.getOperatetime(), "授权时间");
        ValidatorUtil.length(relevanceDto.getOperatorid(), "授权人", 1, 50);
        ValidatorUtil.require(relevanceDto.getFirstid(), "第一个表主键ID");
        ValidatorUtil.length(relevanceDto.getFirstid(), "第一个表主键ID", 1, 50);
        ValidatorUtil.require(relevanceDto.getSecondid(), "第二个表主键ID");
        ValidatorUtil.length(relevanceDto.getSecondid(), "第二个表主键ID", 1, 50);
        ValidatorUtil.length(relevanceDto.getThirdid(), "第三个表主键ID", 1, 50);
        ValidatorUtil.length(relevanceDto.getExtendinfo(), "扩展信息", 1, 100);

        ResponseDto responseDto = new ResponseDto();
        relevanceService.save(relevanceDto);
        responseDto.setContent(relevanceDto);
        return responseDto;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        relevanceService.delete(id);
        return responseDto;
    }
}
